"use strict";
module.exports = (sequelize, DataTypes) =>
  sequelize.define(
    "user",
    {
      id: {
        type: DataTypes.BIGINT(11),
        autoIncrement: true,
        primaryKey: true,
        unique: true
      },
      loginname: {
        type: DataTypes.STRING,
        field: "user_loginname",
        comment: "登录帐号",
        allowNull: false,
        set(val) {
          if (!val || val == "null" || this.getDataValue("email")) {
            this.setDataValue(
              "loginname",
              this.getDataValue("mobile") || this.getDataValue("email")
            );
          } else {
            this.setDataValue("loginname", val);
          }
        }
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "登录密码",
        validate: {
          is: [/^[a-zA-Z0-9+.]{8,12}$/i],
          len: [8, 12],
          notEmpty: true
        }
      },
      username: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "用户名",
        validate: {
          len: [4, 12]
        },
        set(val) {
          if (!val || val == "null" || this.getDataValue("email")) {
            this.setDataValue(
              "username",
              this.getDataValue("mobile") || this.getDataValue("email")
            );
          } else {
            this.setDataValue("username", val);
          }
        }
      },
      mobile: {
        type: DataTypes.STRING,
        field: "user_mobile",
        // unique: true,
        allowNull: false,
        // isEven: function(val) {
        //   // 自定义验证
        //   let reg = /^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/;
        //   if (reg.test(val)) {
        //     this.setDataValue("mobile", val);
        //   } else {
        //     throw new Error("Wrong mobile number");
        //   }
        // }
        validate: {
          is: [
            /^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/i
          ],
          len: [11, 11]
        }
      },
      email: {
        type: DataTypes.STRING,
        field: "user_email",
        allowNull: true,
        validate: {
          // is: [
          //   /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/i
          // ],
          // isEmail: true
        },
        set(val) {
          let reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
          if (val && reg.test(val)) {
            this.setDataValue("email", val);
          } else {
            this.setDataValue("email", "");
          }
        }
      },
      loginTime: {
        type: DataTypes.DATE,
        field: "user_login_time",
        set: function() {
          let lastTime = this.getDataValue("updated_at") || new Date();
          this.setDataValue("loginTime", lastTime);
        }
      },
      lastTime: {
        type: DataTypes.DATE,
        field: "user_last_login_time",
        set: function() {
          let lastTime = this.getDataValue("loginTime") || new Date();
          this.setDataValue("lastTime", lastTime);
        }
      },
      count: {
        type: DataTypes.BIGINT(11),
        field: "user_count",
        // allowNull: false,
        defaultValue: 1,
        set: function(val) {
          if (!val || val == "null") {
            this.setDataValue("count", this.getDataValue("count") + 1);
          }
        }
      },
      active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: "是否正常状态"
      }
    },
    {
      timestamps: true,
      underscored: true,
      // paranoid: true,
      freezeTableName: true,
      tableName: "user",
      charset: "utf8",
      collate: "utf8_general_ci"
    }
  );
